//
//  CyclingstatisticsVC.m
//  KEREN
//
//  Created by MW on 2023/4/6.
//

#import "CyclingstatisticsVC.h"

@interface CyclingstatisticsVC (){
    NSArray *mileage_statics;
}
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) DeviceInfo *deviceInfo;
@property (nonatomic, strong) NSMutableArray *labelArrM;
@property (nonatomic, strong) NSMutableArray *lblArrM;
@property (nonatomic, strong) UIScrollView *dataScrollView;
@property (nonatomic, strong) UILabel *currentGLLbl;
@property (nonatomic, strong) NSMutableArray *lineViewArrM;

@end

@implementation CyclingstatisticsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 100)];
    [self.view addSubview:topView];
    topView.backgroundColor = Whire_Color;
    //TODO:国际化
    NSArray *tipsArr = @[@"累计里程",@"单次最长里程",@"陪伴时间"];
    NSArray *tArr = @[@"--km",@"--km",@"--天"];
    for (int i = 0; i < 3; i ++) {
        UILabel *tipsLbl = [UILabel setLblText:tipsArr[i] font:Font(14) withColor:Color_86898E inSubView:topView];
        tipsLbl.textAlignment = NSTextAlignmentCenter;
        tipsLbl.frame = CGRectMake(DEVICE_WIDTH / 3 * i, 60, DEVICE_WIDTH / 3, 20);
        UILabel *tLbl = [UILabel setLblText:tArr[i] font:Font(32) withColor:Black_Color inSubView:topView];
        tLbl.frame = CGRectMake(DEVICE_WIDTH / 3 * i, 20, DEVICE_WIDTH / 3, 40);
        tLbl.attributedText = [NSAttributedString getAttributeWith:@[@"km",@"天"] string:tArr[i] attributeFont:Font(14)];
        if(i > 0){
          [UIView setLineViewWithframe:CGRectMake( DEVICE_WIDTH / 3 * i - 0.5, 18, 1, 64) forinView:topView];
        }
        [self.lblArrM addObject:tLbl];
    
    }
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, DEVICE_WIDTH, DEVICE_HEIGHT - 100 - 110 - STATUSBAR_HEIGHT)];
    _scrollView.backgroundColor = Main_BackColor;
    [self.view addSubview:_scrollView];
    UILabel *tipsLbl = [UILabel setLblText:@"近15天数据" font:Font_Medium(16) withColor:Color_86898E inSubView:_scrollView];
    [tipsLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(16);
        make.left.offset(24);
    }];
    float backViewWidth = (DEVICE_WIDTH - 48 - 10) /2;
    NSArray *titleArr = @[@"累计里程",@"最高时速",@"平均时速",@"骑行次数"];
    for (int i = 0; i < 4; i++) {
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake( i%2 * (backViewWidth + 10) + 24, 54 + i/2 * 110, backViewWidth, 100)];
        backView.backgroundColor = Main_BackColor;
        backView.layer.cornerRadius = 25;
        backView.clipsToBounds = YES;
        backView.layer.borderWidth = 1;
        backView.layer.borderColor = Color_borderColor;
        UIView *lineView = [UIView setLineViewWithframe:CGRectMake(16, 24, 2, 20) forinView:backView];
        lineView.backgroundColor = Color_7ACC00;
        UILabel *titleLbl = [UILabel setLblText:titleArr[i] font:Font(14) withColor:Color_86898E inSubView:backView];
        [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(24);
            make.height.offset(20);
            make.left.offset(24);
        }];
        UILabel *countLbl = [UILabel setLblText:@"" font:Font(32) withColor:Black_Color inSubView:backView];
        [countLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.top.equalTo(titleLbl.mas_bottom).offset(8);
        }];
        [self.labelArrM addObject:countLbl];
        [_scrollView addSubview:backView];
    }
    _deviceInfo = [UserInfoManager sharedInstance].currentDeviceInfo;
    if(!_deviceInfo){
        if([UserInfoManager sharedInstance].deviceInfoList.count > 0){
            _deviceInfo = [UserInfoManager sharedInstance].deviceInfoList.lastObject;
        }
    }
    if(_deviceInfo){
        [self getDevicecyclingRecode];
        [self getDeviceTrackStatics];
    }else{
        if([UserInfoManager sharedInstance].deviceInfoList.count > 0){
            _deviceInfo = [UserInfoManager sharedInstance].deviceInfoList.lastObject;
        }
    }

}
- (void)getDevicecyclingRecode{
    //Device_cyclingRecode
    WEAKSELF
    NSDictionary *parameters = @{@"ebike_id":@(_deviceInfo.Id)};
    [NetworkingTool POST:Device_cyclingRecode parameters:parameters isDeviceUrl:YES success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
        if([response[@"code"] intValue]  == 0){
            NSDictionary *dict = response[@"data"];
            [weakSelf reloadLblText:dict];
        }else{
           // [[KRAlartManager shareInstance] showErrorAlert:response[@"msg"]];
        }
    } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
        
    }];

}

- (void)getDeviceTrackStatics{
    WEAKSELF
    NSDictionary *parameters = @{@"time_start":@([[DeteManager shareInstance] getLastDay:15]),@"end_time":@([[DeteManager shareInstance] getCurrentDay]),@"ebike_id":@(_deviceInfo.Id)};
    [NetworkingTool POST:Device_track_statics parameters:parameters isDeviceUrl:YES success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
        if([response[@"code"] intValue]  == 0){
            NSDictionary *dict = response[@"data"];
            [weakSelf reloadLabelText:dict];
        }else{
            //[[KRAlartManager shareInstance] showErrorAlert:response[@"msg"]];
        }
    } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
        
    }];
}

- (void)reloadLblText:(NSDictionary *)dict{
    for (int i = 0; i < _lblArrM.count; i ++) {
        UILabel *lbl = _lblArrM[i];
        if(i == 0){
            lbl.text = [NSString stringWithFormat:@"%.2lfkm",[dict[@"total_mileage"] intValue] / 1000.0];
        }else if (i == 1){
            lbl.text = [NSString stringWithFormat:@"%.2lfkm",[dict[@"max_ride_mileage"] intValue] / 1000.0];

        }else if (i == 2){
            lbl.text = [NSString stringWithFormat:@"%d天",[dict[@"accompany_days"] intValue]];

        }
        lbl.attributedText = [NSAttributedString getAttributeWith:@[@"km",@"天"] string:lbl.text attributeFont:Font(14)];
    }

}

- (void)reloadLabelText:(NSDictionary *)dict{
    for (int i = 0; i < _labelArrM.count; i ++) {
        UILabel *lbl = _labelArrM[i];
        if(i == 0){
            lbl.text = [NSString stringWithFormat:@"%.1lfkm",[dict[@"accumulated_mileage"] intValue] / 1000.0];
        }else if (i == 1){
            lbl.text = [NSString stringWithFormat:@"%dkm/h",[dict[@"max_speed"] intValue]];

        }else if (i == 2){
            lbl.text = [NSString stringWithFormat:@"%dkm/h",[dict[@"avg_speed"] intValue]];

        }else{
            lbl.text = [NSString stringWithFormat:@"%d次",[dict[@"cycling_count"] intValue]];
        }
        lbl.attributedText = [NSAttributedString getAttributeWith:@[@"km",@"km/h",@"/h",@"次"] string:lbl.text attributeFont:Font(14)];
    }
    mileage_statics = dict[@"mileage_statics"];
    int maxMileage = 0;
    for (int i = 0; i < mileage_statics.count; i ++) {
        NSDictionary *dict = mileage_statics[i];
        if([dict[@"mileage"] intValue] / 1000 > maxMileage){
            maxMileage = [dict[@"mileage"] intValue] / 1000;
        }
        
    }
    if(maxMileage < 4){
        maxMileage = 4;
    }else if(maxMileage > 4 && maxMileage < 8){
        maxMileage = 8;
    }else if(maxMileage > 8 && maxMileage < 20){
        maxMileage = 20;
    }else if (maxMileage > 20 && maxMileage < 40){
        maxMileage = 40;
    }else if (maxMileage > 40 && maxMileage < 100){
        maxMileage = 100;
    }else {
        maxMileage = 200;
    }
    self.dataScrollView = [[UIScrollView alloc] init];
    self.dataScrollView.frame = CGRectMake(48, 320, DEVICE_WIDTH - 48 - 24, 180);
    [_scrollView addSubview:_dataScrollView];
    _dataScrollView.showsVerticalScrollIndicator = NO;
    _dataScrollView.showsHorizontalScrollIndicator = NO;
    _dataScrollView.bounces = NO;
    [self.lineViewArrM removeAllObjects];
    _currentGLLbl = [[UILabel alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_currentGLLbl];
    _currentGLLbl.backgroundColor = Whire_Color;
    _currentGLLbl.layer.cornerRadius = 16;
    _currentGLLbl.clipsToBounds = YES;
    _currentGLLbl.textColor = Color_7ACC00;
    _currentGLLbl.font = Font_Medium(14);
    _currentGLLbl.textAlignment = NSTextAlignmentCenter;
    for (int i = 0; i< 15; i++) {
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15 + (8 + 42) * i, 160 - 44, 8, 0)];
        lineView.backgroundColor = Color_D9D9D9;
        [_dataScrollView addSubview:lineView];
        for (NSDictionary *dict in mileage_statics) {
            if([dict[@"day"] isEqualToString:[[DeteManager shareInstance] lastDay:14 - i AndType:0]]){
                lineView.frame = CGRectMake(15 + (8 + 42) * i, 160 - 160 * [dict[@"mileage"] intValue] / 1000.0 / maxMileage, 8, 160 * [dict[@"mileage"] intValue] / 1000.0 / maxMileage);
                if(i == 14){
                    if([dict[@"mileage"] intValue] > 0){
                        [_currentGLLbl mas_makeConstraints:^(MASConstraintMaker *make) {
                            make.bottom.equalTo(lineView.mas_top).offset(-6);
                            make.centerX.equalTo(lineView);
                            make.height.offset(32);
                            make.width.offset(78);
                        }];
                        _currentGLLbl.text = [NSString stringWithFormat:@"%.1lfkm",[dict[@"mileage"] intValue] / 1000.0];
                    }
                }
                break;
            }
        }
        if(i == 0){
           UIView *lineViews = [UIView setLineViewWithframe:CGRectMake(47, 320, 1, 160) forinView:self.scrollView];
            lineViews.backgroundColor = rgba(0.53, 0.54, 0.56, 0.4);
            UIView *vlineView = [UIView setLineViewWithframe:CGRectMake(0, 159,  745, 1) forinView:_dataScrollView];
            vlineView.backgroundColor = rgba(0.53, 0.54, 0.56, 0.4);
            lineView.backgroundColor = Color_7ACC00;
        }
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewClick:)];
        [lineView addGestureRecognizer:tap];
        lineView.tag = i;
        lineView.layer.mask = [CAShapeLayer clipsToBounds:lineView.bounds Corners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(4, 4)];
        [self.lineViewArrM addObject:lineView];
        
        UILabel *deteLbl = [[UILabel alloc] init];
        [_dataScrollView addSubview:deteLbl];
        [deteLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(168);
            make.centerX.equalTo(lineView);
        }];
        deteLbl.text = [[DeteManager shareInstance] lastDay:14 - i AndType:1];
        deteLbl.font = Font(10);
        deteLbl.textColor = Color_86898E;
    }
    for (int i = 0; i < 5; i ++) {
        UILabel *kilometerLbl = [[UILabel alloc] init];
        [self.scrollView addSubview:kilometerLbl];
        [kilometerLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_dataScrollView.mas_left).offset(-8);
            make.bottom.equalTo(_dataScrollView.mas_bottom).offset(- (i * 40) - 20);
        }];
        kilometerLbl.font = Font(10);
        kilometerLbl.textColor = Color_86898E;
        kilometerLbl.text = [NSString stringWithFormat:@"%dkm",i * maxMileage / 4];

    }
    _dataScrollView.contentSize = CGSizeMake(745, 0);
    [_dataScrollView setContentOffset:CGPointMake(745 - DEVICE_WIDTH + 48, 0) animated:NO];
    self.scrollView.contentSize = CGSizeMake(0, 560);
}

- (void)tapViewClick:(UITapGestureRecognizer *)tap{
    //tapViewClick
    for (int i = 0; i < _lineViewArrM.count; i++) {
        UIView *view = _lineViewArrM[i];
        if(view.tag == tap.view.tag){
            view.backgroundColor = Color_7ACC00;
        }else{
            view.backgroundColor = Color_D9D9D9;
        }
    }
    [_currentGLLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(tap.view.mas_top).offset(-6);
        make.centerX.equalTo(tap.view);
        make.height.offset(32);
        make.width.offset(78);
    }];
    for (NSDictionary *dict in mileage_statics) {
        if([dict[@"day"] isEqualToString:[[DeteManager shareInstance] lastDay:14 - tap.view.tag AndType:0]]){
            _currentGLLbl.text = [NSString stringWithFormat:@"%.1lfkm",[dict[@"mileage"] intValue] / 1000.0];
            break;
        }
    }
    
}

- (NSMutableArray *)labelArrM{
    if(!_labelArrM){
        _labelArrM = [NSMutableArray array];
    }
    return _labelArrM;
}

- (NSMutableArray *)lblArrM{
    if(!_lblArrM){
        _lblArrM = [NSMutableArray array];
    }
    return _lblArrM;
}

-(NSMutableArray *)lineViewArrM{
    if(!_lineViewArrM){
        _lineViewArrM = [NSMutableArray array];
    }
    return _lineViewArrM;
}



@end
